package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;


    @PostMapping("/status")
    public R<String> updateWithStatus(@RequestParam List<Long> ids){
        log.info("需要修改状态的套餐id:{}",ids);

        return R.success("修改成功");
    }



    /**
     * 批量删除套餐
     * @param ids
     * @return
     */

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("需要删除的套餐id:{}",ids);
        setmealService.removeWithDish(ids);
        return R.success("删除套餐成功");
    }
    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //封装页码和每页显示条数
        Page<Setmeal> setmealPageInfo = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //条件1：模糊查询 条件2：根据修改时间倒叙排序
        wrapper.like(name!=null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //调用套餐service层方法分页查询套餐基本信息
        setmealService.page(setmealPageInfo,wrapper);
        //将SetmealDto进行分装
        Page<SetmealDto> setmealDtoPageInfo = new Page<>();
        //拷贝基本信息
        BeanUtils.copyProperties(setmealPageInfo,setmealDtoPageInfo,"records");
        //获取每页显示的信息
        List<Setmeal> setmealList = setmealPageInfo.getRecords();
/*        ArrayList<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            //遍历循环获取分类id
            Long categoryId = setmeal.getCategoryId();
            //通过分类id获取分类对象
            Category category = categoryService.getById(categoryId);
            //创建setmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(setmeal,setmealDto);
            if(category!=null){
                //将分类名称赋值给setmealDto
                setmealDto.setCategoryName(setmeal.getName());
                //添加分类名称到集合
                setmealDtoList.add(setmealDto);
            }
            setmealDtoPageInfo.setRecords(setmealDtoList);
        }*/
        List<SetmealDto> list = setmealList.stream().map((item) -> {
            //获取分类id
            Long categoryId = item.getCategoryId();
            //通过分类id获取分类对象
            Category category = categoryService.getById(categoryId);
            //创建setmeal对象
            SetmealDto setmealDto = new SetmealDto();
            //拷贝对象
            BeanUtils.copyProperties(item, setmealDto);
            if (category != null) {
                //分类名称赋值
                setmealDto.setCategoryName(item.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        //每页显示信息赋值
        setmealDtoPageInfo.setRecords(list);
        return R.success(setmealDtoPageInfo);
    }



    /**
     * 新增套餐，同时需要保存套餐和菜品的关联关系
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("添加套餐成功");
    }
}
